<?php
namespace app\lib;

use Yii;

class Template
{

    /**
     * 发送模板消息
     *
     * @param string $touser            
     * @param string $shor_id
     *            例如 AT0711
     * @param string $from_id
     *            表单场景下from_id
     * @param array $data
     *            =[
     *            'keyword1' => 'VALUE', //任务名称
     *            'keyword2' => 'VALUE2'
     *            ]
     * @param string $page            
     * @example Template::send("dddd", 'AT0711', "1231231", ['keyvalue'=>1]);
     */
    public static function sendTask($open_id, $data, $page = '')
    {
        $template_id = Yii::$app->params['template']['AT0711'];
        $key = sprintf(CacheConf::DOCTOR_WX_TEMP_FORM_OPENID, $open_id);
        $form_id = Yii::$app->cache->rpop($key);
        if (empty($form_id)) {
            return false;
        }
        try {
            $sendData = [
                'touser' => $open_id,
                'template_id' => $template_id,
                'form_id' => $form_id,
                'data' => $data
            ];
            if ($page) {
                $sendData['page'] = $page;
            }
            $res = Yii::$app->miniprogram->app->template_message->send($sendData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    } 
}